package cn.hxzy.commonUser.service.impl;

import cn.hxzy.commonUser.mapper.RolePerMapper;
import cn.hxzy.commonUser.mapper.UserMapper;

import cn.hxzy.entity.userEntity.RolePer;
import cn.hxzy.entity.userEntity.User;
import cn.hxzy.entity.userEntity.vo.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RolePerMapper rolePerMapper;



    //查询该账户是否存在/有该权限
    @Override
    public UserDetails loadUserByUsername(String userPhone) throws UsernameNotFoundException {


            //2、查询用户信息
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_phone",userPhone);
            User user = userMapper.selectOne(queryWrapper);
            //如果没有查询到用户，就抛出异常
            if(Objects.isNull(user)){
                throw  new RuntimeException("用户名错误");
            }
            //TODO 查询用户对应的权限信息
            //ArrayList<String> list = new ArrayList<>(Arrays.asList("test", "admin"));
            QueryWrapper<RolePer> queryWrapper2=new QueryWrapper<>();
            List<String> rolePerList = rolePerMapper.selectPerById(user.getRoleId());//获取权限对象
            //如果有，把数据封装成UserDetails对象返回
            return new LoginUser(user,rolePerList);

    }
}
